//
//  FontViewController.m
//  ITHome
//
//  Created by lanou3g on 15/7/21.
//  Copyright (c) 2015年 YAX. All rights reserved.
//

#import "FontViewController.h"

@interface FontViewController ()
{
    CGFloat _fontSize;
}
@property (nonatomic, strong) UISlider *fontSlider;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UILabel *leftLabel;
@property (nonatomic, strong) UIImageView *backImageView;

@end

@implementation FontViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self addView];
    // Do any additional setup after loading the view.
}

- (void)addView
{
    //背景图
    self.backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.backImageView.image = [UIImage imageNamed:@"Page3BG"];
    [self.view addSubview:_backImageView];
    
    //滑块试图
    self.fontSlider.frame = CGRectMake((kScreenWidth-kScreenWidth/4*3)/2, kScreenHeight/2, kScreenWidth/4*3, 20);
    self.fontSlider.minimumTrackTintColor = [UIColor orangeColor];
    [self.fontSlider addTarget:self action:@selector(changeValue) forControlEvents:UIControlEventValueChanged];
    
    //当前字体label
    self.leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(_fontSlider.x + _fontSlider.width/2 + 20,_fontSlider.y + 30, _fontSlider.width, 20)];
    self.leftLabel.font = [UIFont systemFontOfSize:9];
    self.leftLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:_leftLabel];
    
    CGFloat size = [[NSUserDefaults standardUserDefaults] floatForKey:@"fontSize"];
//    YAXLog(@"size == %f", size);
 
    
    if (size == 0) {
        self.fontSlider.value = 100;
        self.leftLabel.text = @"当前字体为14号";
    }else{
        self.fontSlider.value = size*200/28;
        self.leftLabel.text = [NSString stringWithFormat:@"当前字体大小为%d",(int)size];
    }
    [self.view addSubview:_fontSlider];
    //字体样式。
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth /5 , kScreenHeight/4, kScreenWidth/5*3, 60)];
    self.label.textColor = [UIColor orangeColor];
//    self.label.backgroundColor = [UIColor randomColor];
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.text = @"IT家园,欢迎您!";
    [self.view addSubview:_label];
 
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(_fontSlider.x + _fontSlider.width/2 - 40, _fontSlider.y + 70, 80, 30);
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button.layer setBorderWidth:1];
    [button.layer setBorderColor:[UIColor orangeColor].CGColor];
    [button.layer setMasksToBounds:YES];
    [button.layer setCornerRadius:10.0];
    button.tintColor = [UIColor orangeColor];
    [button addTarget:self action:@selector(backButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *reButton = [UIButton buttonWithType:UIButtonTypeSystem];
    reButton.frame = CGRectMake(button.x, button.y + 40, 80, 30);
    [reButton.layer setBorderWidth:1];
    [reButton.layer setBorderColor:[UIColor orangeColor].CGColor];
    [reButton.layer setMasksToBounds:YES];
    [reButton.layer setCornerRadius:10.0];
    reButton.tintColor = [UIColor orangeColor];
    [reButton setTitle:@"默认" forState:UIControlStateNormal];
    [reButton addTarget:self action:@selector(reButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:reButton];
}

- (void)changeValue
{
    _fontSize = 14 * _fontSlider.value / 100;
    _label.font = [UIFont systemFontOfSize:_fontSize];
    NSString *fSize = [NSString stringWithFormat:@"当前字体大小为%d",(int)_fontSize];
    _leftLabel.text = fSize;
}

- (void)reButton
{
    _fontSize = 14;
    [[NSUserDefaults standardUserDefaults] setFloat:_fontSize forKey:@"fontSize"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    self.block(_fontSize);
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)backButton
{
    if (_fontSize == 0) {
        _fontSize = 14*_fontSlider.value/100;
    }
    [[NSUserDefaults standardUserDefaults] setFloat:_fontSize forKey:@"fontSize"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    self.block(_fontSize);
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 内容警告触发方法
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [[SDImageCache sharedImageCache] clearMemory];
}

- (UISlider *)fontSlider
{
    if (!_fontSlider) {
        _fontSlider = [[UISlider alloc] init];
        self.fontSlider.minimumValue = 0;
        self.fontSlider.maximumValue = 200;
    }
    return _fontSlider;
}



@end
